package com.yunke.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunke.common.log.BusinessLog;
import com.yunke.common.result.YunkeResult;
import com.yunke.common.security.RequirePermission;
import com.yunke.dto.dazi.YunkeDaziCreateDto;
import com.yunke.dto.dazi.YunkeDaziJoinDto;
import com.yunke.dto.dazi.YunkeDaziListDto;
import com.yunke.service.YunkeDaziService;
import com.yunke.vo.dazi.YunkeDaziDetailVo;
import com.yunke.vo.dazi.YunkeDaziListVo;
import com.yunke.vo.dazi.YunkeTabClassVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * 云科搭子管理控制器
 * 
 * @author yunke
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequestMapping("/api/dazi")
@Tag(name = "搭子管理", description = "搭子发布、参与、查询等功能")
@Validated
public class YunkeDaziController {

    @Autowired
    private YunkeDaziService daziService;

    @GetMapping("/category/list")
    @Operation(summary = "获取搭子分类列表", description = "获取所有启用的搭子分类列表")
    public YunkeResult<List<YunkeTabClassVo>> getCategoryList() {
        List<YunkeTabClassVo> categoryList = daziService.getCategoryList();
        return YunkeResult.success(categoryList);
    }

    @PostMapping("/create")
    @Operation(summary = "发布搭子", description = "用户发布新的搭子活动")
    @BusinessLog("发布搭子")
    @RequirePermission
    public YunkeResult<Long> createDazi(@Valid @RequestBody YunkeDaziCreateDto createDto) {
        Long daziId = daziService.createDazi(createDto);
        return YunkeResult.success(daziId, "发布搭子成功");
    }

    @PostMapping("/list")
    @Operation(summary = "获取搭子列表", description = "根据条件查询搭子列表，支持分页")
    public YunkeResult<Page<YunkeDaziListVo>> getDaziList(@Valid @RequestBody YunkeDaziListDto listDto) {
        Page<YunkeDaziListVo> daziPage = daziService.getDaziList(listDto);
        return YunkeResult.success(daziPage);
    }

    @GetMapping("/detail/{daziId}")
    @Operation(summary = "获取搭子详情", description = "根据ID获取搭子详细信息")
    public YunkeResult<YunkeDaziDetailVo> getDaziDetail(
            @Parameter(description = "搭子ID", required = true)
            @PathVariable @NotNull(message = "搭子ID不能为空") Long daziId) {
        YunkeDaziDetailVo detailVo = daziService.getDaziDetail(daziId);
        return YunkeResult.success(detailVo);
    }

    @PostMapping("/join")
    @Operation(summary = "参与搭子", description = "申请参与指定的搭子活动")
    @BusinessLog("参与搭子")
    @RequirePermission
    public YunkeResult<Boolean> joinDazi(@Valid @RequestBody YunkeDaziJoinDto joinDto) {
        boolean result = daziService.joinDazi(joinDto);
        return YunkeResult.success(result, "申请参与搭子成功");
    }

    @PostMapping("/quit/{daziId}")
    @Operation(summary = "退出搭子", description = "退出已参与的搭子活动")
    @BusinessLog("退出搭子")
    @RequirePermission
    public YunkeResult<Boolean> quitDazi(
            @Parameter(description = "搭子ID", required = true)
            @PathVariable @NotNull(message = "搭子ID不能为空") Long daziId,
            @Parameter(description = "退出原因")
            @RequestParam(required = false) String quitReason) {
        boolean result = daziService.quitDazi(daziId, quitReason);
        return YunkeResult.success(result, "退出搭子成功");
    }

    @GetMapping("/my/published")
    @Operation(summary = "获取我发布的搭子", description = "获取当前用户发布的搭子列表")
    @RequirePermission
    public YunkeResult<Page<YunkeDaziListVo>> getMyPublishedDazi(
            @Parameter(description = "当前页", example = "1")
            @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量", example = "10")
            @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "状态筛选: 1=已发布,0=草稿")
            @RequestParam(required = false) Integer status) {
        Page<YunkeDaziListVo> daziPage = daziService.getMyPublishedDazi(current, size, status);
        return YunkeResult.success(daziPage);
    }

    @GetMapping("/my/joined")
    @Operation(summary = "获取我参与的搭子", description = "获取当前用户参与的搭子列表")
    @RequirePermission
    public YunkeResult<Page<YunkeDaziListVo>> getMyJoinedDazi(
            @Parameter(description = "当前页", example = "1")
            @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量", example = "10")
            @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "状态筛选: 0=待审核,1=已通过,2=已拒绝")
            @RequestParam(required = false) Integer status) {
        Page<YunkeDaziListVo> daziPage = daziService.getMyJoinedDazi(current, size, status);
        return YunkeResult.success(daziPage);
    }

    @PutMapping("/edit/{daziId}")
    @Operation(summary = "编辑搭子", description = "编辑已发布的搭子信息")
    @BusinessLog("编辑搭子")
    @RequirePermission
    public YunkeResult<Boolean> editDazi(
            @Parameter(description = "搭子ID", required = true)
            @PathVariable @NotNull(message = "搭子ID不能为空") Long daziId,
            @Valid @RequestBody YunkeDaziCreateDto updateDto) {
        boolean result = daziService.editDazi(daziId, updateDto);
        return YunkeResult.success(result, "编辑搭子成功");
    }

    @DeleteMapping("/delete/{daziId}")
    @Operation(summary = "删除搭子", description = "删除已发布的搭子")
    @BusinessLog("删除搭子")
    @RequirePermission
    public YunkeResult<Boolean> deleteDazi(
            @Parameter(description = "搭子ID", required = true)
            @PathVariable @NotNull(message = "搭子ID不能为空") Long daziId) {
        boolean result = daziService.deleteDazi(daziId);
        return YunkeResult.success(result, "删除搭子成功");
    }

    @PostMapping("/top/{daziId}")
    @Operation(summary = "搭子置顶", description = "设置搭子置顶显示")
    @BusinessLog("搭子置顶")
    @RequirePermission
    public YunkeResult<Boolean> topDazi(
            @Parameter(description = "搭子ID", required = true)
            @PathVariable @NotNull(message = "搭子ID不能为空") Long daziId,
            @Parameter(description = "置顶天数", example = "7")
            @RequestParam(defaultValue = "7") Integer topDays) {
        boolean result = daziService.topDazi(daziId, topDays);
        return YunkeResult.success(result, "搭子置顶成功");
    }

    @PostMapping("/audit/{daziId}")
    @Operation(summary = "搭子审核", description = "管理员审核搭子")
    @BusinessLog("搭子审核")
    @RequirePermission
    public YunkeResult<Boolean> auditDazi(
            @Parameter(description = "搭子ID", required = true)
            @PathVariable @NotNull(message = "搭子ID不能为空") Long daziId,
            @Parameter(description = "审核状态: 1=通过,2=拒绝", required = true)
            @RequestParam @NotNull(message = "审核状态不能为空") Integer auditStatus,
            @Parameter(description = "审核备注")
            @RequestParam(required = false) String auditRemark) {
        boolean result = daziService.auditDazi(daziId, auditStatus, auditRemark);
        return YunkeResult.success(result, "搭子审核成功");
    }

    @PostMapping("/audit/join/{joinId}")
    @Operation(summary = "审核参与申请", description = "搭子发布者审核参与申请")
    @BusinessLog("审核参与申请")
    @RequirePermission
    public YunkeResult<Boolean> auditJoinApplication(
            @Parameter(description = "参与记录ID", required = true)
            @PathVariable @NotNull(message = "参与记录ID不能为空") Long joinId,
            @Parameter(description = "审核状态: 1=通过,2=拒绝", required = true)
            @RequestParam @NotNull(message = "审核状态不能为空") Integer auditStatus,
            @Parameter(description = "审核备注")
            @RequestParam(required = false) String auditRemark) {
        boolean result = daziService.auditJoinApplication(joinId, auditStatus, auditRemark);
        return YunkeResult.success(result, "审核申请成功");
    }

    @PostMapping("/like/{daziId}")
    @Operation(summary = "点赞搭子", description = "点赞或取消点赞搭子")
    @BusinessLog("点赞搭子")
    @RequirePermission
    public YunkeResult<Boolean> likeDazi(
            @Parameter(description = "搭子ID", required = true)
            @PathVariable @NotNull(message = "搭子ID不能为空") Long daziId) {
        boolean result = daziService.likeDazi(daziId);
        return YunkeResult.success(result, "操作成功");
    }

    @GetMapping("/nearby")
    @Operation(summary = "获取附近搭子", description = "根据地理位置获取附近的搭子")
    public YunkeResult<Page<YunkeDaziListVo>> getNearbyDazi(
            @Parameter(description = "经度", required = true)
            @RequestParam @NotNull(message = "经度不能为空") String longitude,
            @Parameter(description = "纬度", required = true)
            @RequestParam @NotNull(message = "纬度不能为空") String latitude,
            @Parameter(description = "搜索半径(km)", example = "10")
            @RequestParam(defaultValue = "10") Double radius,
            @Parameter(description = "当前页", example = "1")
            @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量", example = "10")
            @RequestParam(defaultValue = "10") Integer size) {
        Page<YunkeDaziListVo> daziPage = daziService.getNearbyDazi(longitude, latitude, radius, current, size);
        return YunkeResult.success(daziPage);
    }

    @GetMapping("/hot")
    @Operation(summary = "获取热门搭子", description = "获取热门推荐搭子列表")
    public YunkeResult<Page<YunkeDaziListVo>> getHotDazi(
            @Parameter(description = "当前页", example = "1")
            @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量", example = "10")
            @RequestParam(defaultValue = "10") Integer size) {
        Page<YunkeDaziListVo> daziPage = daziService.getHotDazi(current, size);
        return YunkeResult.success(daziPage);
    }

    @GetMapping("/search")
    @Operation(summary = "搜索搭子", description = "根据关键词搜索搭子")
    public YunkeResult<Page<YunkeDaziListVo>> searchDazi(
            @Parameter(description = "搜索关键词", required = true)
            @RequestParam @NotNull(message = "搜索关键词不能为空") String keyword,
            @Parameter(description = "当前页", example = "1")
            @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量", example = "10")
            @RequestParam(defaultValue = "10") Integer size) {
        Page<YunkeDaziListVo> daziPage = daziService.searchDazi(keyword, current, size);
        return YunkeResult.success(daziPage);
    }
} 